package morenoapp.model;

import com.jgoodies.binding.beans.Model;
import java.awt.Graphics;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.List;
import morenoapp.components.thinkgraphv2.ThinkgraphBaseCanvas;
import morenoapp.model.data.MorenoQuestionFace;
import morenoapp.model.data.MorenoRespondentFace;
import morenoapp.model.impl.MorenoView2Impl;
import morenoapp.model.view2.MorenoView2Data;
import morenoapp.model.view2.MorenoView2Face;

/**
 *
 * @author dmitry.mamonov
 */
public class MorenoView2Wrapper extends Model implements MorenoView2Face {
    List<MorenoView2Data> modelsList = null;
    MorenoView2Data model = null;

    //generated:begin
    @Override
    public int getDisplayFirstNRelationships() {
        if (model==null) return 1;
        return model.getDisplayFirstNRelationships();
    }


    @Override
    public Point2D getRespondentPosition(MorenoRespondentFace _value) {
        if (model==null) return null;
        return model.getRespondentPosition(_value);
    }


    @Override
    public int getRingStep() {
        if (model==null) return 10;
        return model.getRingStep();
    }


    @Override
    public String getViewName() {
        if (model==null) return "<empty>";
        return model.getViewName();
    }


    @Override
    public boolean isDisplayCorrections() {
        if (model==null) return false;
        return model.isDisplayCorrections();
    }


    @Override
    public boolean isDisplayMetrics() {
        if (model==null) return false;
        return model.isDisplayMetrics();
    }


    @Override
    public boolean isDisplayNames() {
        if (model==null) return false;
        return model.isDisplayNames();
    }


    @Override
    public boolean isDisplayRelationships(MorenoQuestionFace _value) {
        if (model==null) return false;
        return model.isDisplayRelationships(_value);
    }


    @Override
    public boolean isDisplaySex() {
        if (model==null) return false;
        return model.isDisplaySex();
    }


    @Override
    public boolean isDisplayTooltips() {
        if (model==null) return false;
        return model.isDisplayTooltips();
    }


    @Override
    public boolean isDivideBySex() {
        if (model==null) return false;
        return model.isDivideBySex();
    }


    @Override
    public boolean isHideFemale() {
        if (model==null) return false;
        return model.isHideFemale();
    }


    @Override
    public boolean isHideMale() {
        if (model==null) return false;
        return model.isHideMale();
    }


    @Override
    public boolean isHideRespondent(MorenoRespondentFace _value) {
        if (model==null) return false;
        return model.isHideRespondent(_value);
    }


    @Override
    public boolean isHideRespondentOrSex(MorenoRespondentFace _value) {
        if (model==null) return false;
        return model.isHideRespondentOrSex(_value);
    }


    @Override
    public void setDisplayCorrections(boolean _value) {
        System.out.println("model.displayCorrections is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isDisplayCorrections();
        model.setDisplayCorrections(_value);
        firePropertyChange("displayCorrections", oldValue, _value);
    }


    @Override
    public void setDisplayFirstNRelationships(int _value) {
        System.out.println("model.displayFirstNRelationships is set to :"+_value);
        checkModelExists();
        int oldValue = model.getDisplayFirstNRelationships();
        model.setDisplayFirstNRelationships(_value);
        firePropertyChange("displayFirstNRelationships", oldValue, _value);
    }


    @Override
    public void setDisplayMetrics(boolean _value) {
        System.out.println("model.displayMetrics is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isDisplayMetrics();
        model.setDisplayMetrics(_value);
        firePropertyChange("displayMetrics", oldValue, _value);
    }


    @Override
    public void setDisplayNames(boolean _value) {
        System.out.println("model.displayNames is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isDisplayNames();
        model.setDisplayNames(_value);
        firePropertyChange("displayNames", oldValue, _value);
    }


    @Override
    public void setDisplayRelationships(MorenoQuestionFace _key, boolean _value) {
        System.out.println("model.displayRelationships is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isDisplayRelationships(_key);
        model.setDisplayRelationships(_key, _value);
        firePropertyChange("displayRelationships", oldValue, _value);
    }


    @Override
    public void setDisplaySex(boolean _value) {
        System.out.println("model.displaySex is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isDisplaySex();
        model.setDisplaySex(_value);
        firePropertyChange("displaySex", oldValue, _value);
    }


    @Override
    public void setDisplayTooltips(boolean _value) {
        System.out.println("model.displayTooltips is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isDisplayTooltips();
        model.setDisplayTooltips(_value);
        firePropertyChange("displayTooltips", oldValue, _value);
    }


    @Override
    public void setDivideBySex(boolean _value) {
        System.out.println("model.divideBySex is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isDivideBySex();
        model.setDivideBySex(_value);
        firePropertyChange("divideBySex", oldValue, _value);
    }


    @Override
    public void setHideFemale(boolean _value) {
        System.out.println("model.hideFemale is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isHideFemale();
        model.setHideFemale(_value);
        firePropertyChange("hideFemale", oldValue, _value);
    }


    @Override
    public void setHideMale(boolean _value) {
        System.out.println("model.hideMale is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isHideMale();
        model.setHideMale(_value);
        firePropertyChange("hideMale", oldValue, _value);
    }


    @Override
    public void setHideRespondent(MorenoRespondentFace _key, boolean _value) {
        System.out.println("model.hideRespondent is set to :"+_value);
        checkModelExists();
        boolean oldValue = model.isHideRespondent(_key);
        model.setHideRespondent(_key, _value);
        firePropertyChange("hideRespondent", oldValue, _value);
    }


    @Override
    public void setRespondentPosition(MorenoRespondentFace _key, Point2D _value) {
        System.out.println("model.respondentPosition is set to :"+_value);
        checkModelExists();
        Point2D oldValue = model.getRespondentPosition(_key);
        model.setRespondentPosition(_key, _value);
        firePropertyChange("respondentPosition", oldValue, _value);
    }


    @Override
    public void setRingStep(int _value) {
        System.out.println("model.ringStep is set to :"+_value);
        checkModelExists();
        int oldValue = model.getRingStep();
        model.setRingStep(_value);
        firePropertyChange("ringStep", oldValue, _value);
    }


    @Override
    public void setViewName(String _value) {
        System.out.println("model.viewName is set to :"+_value);
        checkModelExists();
        String oldValue = model.getViewName();
        model.setViewName(_value);
        firePropertyChange("viewName", oldValue, _value);
    }


    protected void firePropertiesAllChanged() {
        firePropertyChange("displayFirstNRelationships",null, getDisplayFirstNRelationships());
        firePropertyChange("respondentPosition",null, new Object());
        firePropertyChange("ringStep",null, getRingStep());
        firePropertyChange("viewName",null, getViewName());
        firePropertyChange("displayCorrections",null, isDisplayCorrections());
        firePropertyChange("displayMetrics",null, isDisplayMetrics());
        firePropertyChange("displayNames",null, isDisplayNames());
        firePropertyChange("displayRelationships",null, new Object());
        firePropertyChange("displaySex",null, isDisplaySex());
        firePropertyChange("displayTooltips",null, isDisplayTooltips());
        firePropertyChange("divideBySex",null, isDivideBySex());
        firePropertyChange("hideFemale",null, isHideFemale());
        firePropertyChange("hideMale",null, isHideMale());
        firePropertyChange("hideRespondent",null, new Object());
        firePropertyChange("hideRespondentOrSex",null, new Object());
    }
    //generated:end

    protected void checkModelExists() {
//        return model!=null;
        if (model==null){
            throw new IllegalStateException("Delegate is not initialized");
        }
    }
    @Override
    public String getViewName(int _index) {
        checkModelExists();
        return modelsList.get(_index).getViewName();
    }

    @Override
    public int getViewsCount() {
        if (modelsList==null){
            return 0;
        } else {
            return modelsList.size();
        }
    }

    @Override
    public int getCurrentView() {
        checkModelExists();
        if (modelsList!=null){
            return modelsList.indexOf(model);
        } else {
            return -1;
        }
    }

    @Override
    public void setCurrentView(int _index) {
        if (modelsList!=null){
            model = modelsList.get(_index);
        } else {
            model = new MorenoView2Impl();
        }
        firePropertiesAllChanged();
    }

    @Override
    public void addView(String _name, boolean _copyFromCurrent) {
        MorenoView2Impl newModel = new MorenoView2Impl();
        newModel.setViewName(_name);
        modelsList.add(newModel);
        firePropertiesAllChanged();
    }

    @Override
    public void deleteView(int _index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setDelegate(List<MorenoView2Data> _delegate){
        this.modelsList = _delegate;
        setCurrentView(0);
    }
    @Override
    public BufferedImage getRenderedView() {
        ThinkgraphBaseCanvas canvas = new ThinkgraphBaseCanvas(this);
        canvas.updateCanvasFromModel();
        int size = (this.getRingStep()*4)*2;
        canvas.setBounds(0, 0, size, size);
        BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();
        canvas.paint(g);
        g.dispose();
        return img;
    }
}
